<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function __construct($application) {
        $admin = new Zend_Controller_Router_Route(
                        'admin123/:controller/:action/*',
                        array('module' => 'admin', 'controller' => 'index', 'action' => 'index')
        );

        $image = new Zend_Controller_Router_Route_Regex(
                        'images/(.+)/(\d+)_(\d+)_(\d+).(.+)',
                        array(
                            'controller' => 'image',
                            'action' => 'image',
                            'module' => 'default',
                            'overScale' => '0',
                            'type' => 'jpg',
                        ),
                        array(
                            1 => 'id',
                            2 => 'width',
                            3 => 'height',
                            4 => 'overScale',
                            5 => 'type',
                        ),
                        'images/%s/%d_%d_%d.%s'
        );

        $offerImage = new Zend_Controller_Router_Route(
                        ':controller/:action/*',
                        array('module' => 'default', 'controller' => 'offer', 'action' => 'image')
        );
        $catalogImage = new Zend_Controller_Router_Route(
                        ':controller/:action/*',
                        array('module' => 'default', 'controller' => 'catalog', 'action' => 'image')
        );

        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();
        $router->addDefaultRoutes();
        $default = $router->getRoute('default');
        $router->addRoute('offerImage', $offerImage);
        $router->addRoute('catalogImage', $offerImage);
        $router->addRoute('default', $default);
        $router->addRoute('page', $default);
        $router->addRoute('search', $default);
        $router->addRoute('login', $default);
        $router->addRoute('admin', $admin);
        $router->addRoute('image', $image);
        parent::__construct($application);
    }

    protected function _initAutoload() {
        $autoloader = new Zend_Loader_Autoloader_Resource(array(
                    'namespace' => 'Admin_',
                    'basePath' => APPLICATION_PATH . '/../modules/admin',
                    'resourceTypes' => array(
                        'form' => array(
                            'path' => 'forms',
                            'namespace' => 'Form',
                        ),
                        'model' => array(
                            'path' => 'models',
                            'namespace' => 'Model',
                        ),
                    )
                ));
        return $autoloader;
    }

    protected function _initDoctype() {
        $doctypeHelper = new Zend_View_Helper_Doctype();
        $doctypeHelper->doctype(Zend_View_Helper_Doctype::XHTML5);
    }

    protected function _initSetBaseUrl() {
        Zend_Controller_Front::getInstance()->setBaseUrl('http://' . $_SERVER['HTTP_HOST']);
    }

    protected function _initConfig() {
        Zend_Registry::set('config', $this->getOptions());
    }

    protected function _initView() {
        // Initialize view
        $view = new Zend_View();

        $view->doctype('XHTML1_TRANSITIONAL');

        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);

        $viewHelperPaths = $this->_pluginResources['view']['helperPath'];
        foreach ($viewHelperPaths as $cls => $path) {
            $view->addHelperPath($path, $cls);
        }
        $view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'Application_View_Helper');
        $view->addScriptPath(APPLICATION_PATH . '/views/scripts');

        // Return it, so that it can be stored by the bootstrap
        return $view;
    }

    protected function _initLocale() {

        $sessionLang = new Zend_Session_Namespace('Lang');
//        $sessionLang->lang = 'en_EN';
        $sessionLang->lang = 'pl_PL';

        if ($sessionLang->lang == 'pl_PL') {
            $locale = new Zend_Locale('pl_PL');
        } else {
            $locale = new Zend_Locale('en_EN');

            if ($sessionLang->lang == '') {
                //echo 'Zapisuję lang en';
                $sessionLang->lang = 'en_EN';
            }
        }
        //Zend_Debug::dump($sessionLang->lang);
        //$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/languages/translation.log');
        $writer = new Zend_Log_Writer_Firebug();
        $log = new Zend_Log($writer);

        $translate = new Zend_Translate('gettext', APPLICATION_PATH . '/languages/' . $sessionLang->lang . '.mo', $sessionLang->lang);
        //$translate->addTranslation(APPLICATION_PATH . '/languages/pl_PL.mo', 'pl');

        $translate->setLocale($locale);

        $translate->setOptions(
                array(
                    //'disableNotices' => true,
                    'log' => $log,
                    'logMessage' => "Brakuje '%message%' w języku '%locale%'",
                    'logPriority' => Zend_Log::ALERT,
                    'logUntranslated' => true
                )
        );

        //Zend_Debug::dump($translate);
        Zend_Locale::setDefault($locale);
        Zend_Registry::set('Zend_Locale', $locale);
        Zend_Registry::set('Zend_Translate', $translate);
        $translate->translate('XXX_LANG');
    }

//    public function _initLogger() {
//        $name = date('Y-m-d').'.txt';
//        $path = APPLICATION_PATH.'/logs/'.$name;
//        $writer = new Zend_Log_Writer_Stream($path);
//        $logger = new Zend_Log($writer);
//        Zend_Registry::set('logger', $logger);
//        $logger->log('***** Bootstrap *****', Zend_Log::EMERG);
//    }
}

